﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;

public partial class Admin_Power_PowerManager : System.Web.UI.Page
{
    PowerManager dllPowerManager = new PowerManager();
    RoleManager dllRoleManager = new RoleManager();
    Model.UserInfo objUserInfo = new Model.UserInfo();
    Authentication dllAuthentication = new Authentication();
    DataTable _powerTable = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        dllAuthentication.isAdminLogin();
        this.btntijao.Enabled = false;
        _powerTable = dllAuthentication.getPower(objUserInfo.UserName, "权限管理");
        for (int i = 0; i < _powerTable.Rows.Count; i++)
        {
            if (_powerTable.Rows[i]["PowerName"].ToString() == "新建")
            {
                this.btntijao.Enabled = true;
            }
            if (_powerTable.Rows[i]["PowerName"].ToString() == "删除")
            {
            }
            if (_powerTable.Rows[i]["PowerName"].ToString() == "查看")
            {
            }
            if (_powerTable.Rows[i]["PowerName"].ToString() == "修改")
            {
                this.btntijao.Enabled = true;
            }
        }
        if (!IsPostBack)
        {
            BindDropDownList();
            if (this.drpRoleName.Items.Count > 0)
            {
                BindRadioButtonList(this.rdbModule);
                BindRadioButtonList(this.rdbChildModule);
                BindCheckBoxList();
            }
            else
            {
                Utility.HttpHelp.alert("请先添加角色组！", Page);
            }
        }
    }
   




    /// <summary>
    /// 绑定RadioButtonList
    /// </summary>
    /// <author>grokyao</author>
    /// <date>2009-1-8</date>
    void BindRadioButtonList(RadioButtonList _rbl)
    {
        if (_rbl.Equals(this.rdbModule))
        {
            this.rdbModule.DataSource = dllPowerManager.getPowerDetail();
            this.rdbModule.DataTextField = "ModuleName";
            this.rdbModule.DataBind();
            this.rdbModule.Items[0].Selected = true;
        }
        if (_rbl.Equals(this.rdbChildModule))
        {
            this.rdbChildModule.DataSource = dllPowerManager.getChildModuleByParentModule(this.rdbModule.SelectedItem.Text);
            this.rdbChildModule.DataTextField = "ModuleName";
            this.rdbChildModule.DataBind();
            if (this.rdbChildModule.Items.Count > 0)
            {
                this.rdbChildModule.Items[0].Selected = true;
            }
        }
    }


    /// <summary>
    /// 绑定权限名称CheckBoxList
    /// </summary>
    /// <author>grokyao</author>
    /// <date>2009-1-8</date>
    void BindCheckBoxList()
    {
        this.ckbPowerName.Items.Clear();
        ListItem _Add = new ListItem("新建");
        _Add.Selected = false;
        ListItem _Del = new ListItem("删除");
        _Del.Selected = false;
        ListItem _Update = new ListItem("修改");
        _Update.Selected = false;
        ListItem _Select = new ListItem("查看");
        _Select.Selected = false;
        this.ckbPowerName.Items.Add(_Add);
        this.ckbPowerName.Items.Add(_Del);
        this.ckbPowerName.Items.Add(_Update);
        this.ckbPowerName.Items.Add(_Select);

        object[] _powerValue = new object[2];
        _powerValue[0] = this.drpRoleName.SelectedItem.Text;
        if (this.rdbChildModule.Items.Count > 0)
        {
            _powerValue[1] = this.rdbChildModule.SelectedItem.Text;
        }
        else
        {
            _powerValue[1] = this.rdbModule.SelectedItem.Text;
        }
        DataTable _power = dllPowerManager.getPowerByRoleNameAndModuleName(_powerValue);
        for (int i = 0; i < this.ckbPowerName.Items.Count; i++)
        {
            for (int j = 0; j < _power.Rows.Count; j++)
            {
                if (this.ckbPowerName.Items[i].Text == _power.Rows[j]["PowerName"].ToString())
                {
                    this.ckbPowerName.Items[i].Selected = true;
                }
            }
        }
    }


    /// <summary>
    /// 绑定DropDownList
    /// </summary>
    /// <author>grokyao</author>
    /// <date>2009-1-8</date>
    void BindDropDownList()
    {
        this.drpRoleName.DataSource = dllRoleManager.get();
        this.drpRoleName.DataTextField = "RoleName";
        this.drpRoleName.DataValueField = "RoleId";
        this.drpRoleName.DataBind();
        
    }


   


    /// <summary>
    /// 选择模块事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-1-8</date>
    protected void rdbModule_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListItem _parentModule = this.rdbModule.SelectedItem;
        if (_parentModule != null)
        {
            BindRadioButtonList(this.rdbChildModule);
            BindCheckBoxList();
        }
    }

    


    /// <summary>
    /// 提交按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    ///<author>gzz</author>
    ///<date>2008-12-31 </date>
    protected void btntijao_Click(object sender, EventArgs e)
    {
        //计数器变量(累计总共选择项)        
        int result = 0;        
        for (int i = 0; i < this.ckbPowerName.Items.Count; i++)
        {
            object[] _power = new object[3];
            object[] _value = new object[3];
            _value[0] = this.drpRoleName.SelectedItem.Text;

            _power[0] = this.drpRoleName.SelectedItem.Text;
            if (this.rdbChildModule.Items.Count > 0)
            {
                _power[1] = this.rdbChildModule.SelectedItem.Text;
                _value[1] = this.rdbChildModule.SelectedItem.Text;
            }
            else
            {
                _power[1] = this.rdbModule.SelectedItem.Text;
                _value[1] = this.rdbModule.SelectedItem.Text;
            }
            _power[2] = this.ckbPowerName.Items[i].Text;
            _value[2] = this.ckbPowerName.Items[i].Text;
            DataTable _powerTable = dllPowerManager.getPowerByRoleNameAndModuleNameAndPowerName(_power);
            if (_powerTable.Rows.Count > 0)
            {
                //修改
                if (this.ckbPowerName.Items[i].Selected == true)
                {
                    //增加
                    result = dllPowerManager.editByRoleNameAndModuleNameUse(_value);
                }
                else
                {
                    //删除
                    result = dllPowerManager.editByRoleNameAndModuleNameUnUse(_value);
                }
                
                if (result == 0)
                {
                    Utility.Error.RedirectErrorPage("修改权限未成功，请重试！");
                }                
            }
            else
            {
                //添加
                result = dllPowerManager.add(_value);
                if (result == 0)
                {
                    Utility.Error.RedirectErrorPage("添加权限未成功，请重试！");
                }               
            }
        }
        BindCheckBoxList();
    }



    /// <summary>
    /// 重置按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnchongzhi_Click(object sender, EventArgs e)
    {
        Response.Redirect("PowerManager.aspx");
    }
    protected void drpRoleName_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListItem _li = this.drpRoleName.SelectedItem;
        if (_li != null)
        {
            BindRadioButtonList(this.rdbModule);
            BindRadioButtonList(this.rdbChildModule);
            BindCheckBoxList();
        }
    }


    /// <summary>
    /// 子模块索引改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rdbChildModule_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindCheckBoxList();
    }
}
